#include "Hmi/hmi.h"
#include "ui_robcontrollerhmi.h"

Hmi::Hmi(){
    ui = new Ui::RobControllerHMI;
    this->ui->setupUi(this);

    this->uiCoordinateAdjusts = nullptr;
    this->uiRobotDataView = new RobotDataView();

    this->pDocumentModel = new QFileSystemModel();

    this->ui->DocumnetTree->setModel(pDocumentModel);
    this->ui->DocumnetTree->setRootIndex(pDocumentModel->index("../SCRIPT"));
    this->ui->stackedWidget->insertWidget(2,uiRobotDataView);

    connect(ui->HomeNew, SIGNAL(clicked(bool)), this, SLOT(onbtnclicked_HomeNew()));
    connect(ui->HomeOpen, SIGNAL(clicked(bool)), this, SLOT(onbtnclicked_HomeOpen()));
    connect(ui->HomeSelect, SIGNAL(clicked(bool)), this, SLOT(onbtnclicked_HomeSelect()));
}

Hmi::~Hmi(){
    if(this->ui != nullptr)
        delete ui;
    if(this->pDocumentModel != nullptr)
        delete this->pDocumentModel;
    if(uiCoordinateAdjusts != nullptr)
        delete this->uiCoordinateAdjusts;
    if(uiRobotDataView != nullptr)
        delete this->uiRobotDataView;

}

void Hmi::FastTimerLoop(){
    this->fasttimer_RoboticOpenGLViewUpdate();
}

void Hmi::SlowTimerLoop(){

}

void Hmi::onbtnclicked_Home(){
}
void Hmi::onbtnclicked_Setting(){
}
void Hmi::onbtnclicked_Edit(){
}
void Hmi::onbtnclicked_MainScriptStatus(){
}
void Hmi::onbtnclicked_SubScriptStatus(){
}
void Hmi::onbtnclicked_VisHmi(){
    if(this->uiRobotDataView != nullptr){
        this->ui->stackedWidget->setCurrentIndex(2);
    }
}

void Hmi::onbtnclicked_Coordinate(){
    if(this->uiCoordinateAdjusts == nullptr){
        this->uiCoordinateAdjusts = new DialogCoordinateAdjust();
        this->uiCoordinateAdjusts->setHandle(this->pBackend);
        this->uiCoordinateAdjusts->exec();
        delete this->uiCoordinateAdjusts;
        this->uiCoordinateAdjusts = nullptr;
    }
}

void Hmi::fasttimer_RoboticOpenGLViewUpdate(){
    if(this->ui->stackedWidget->currentIndex() == 2){
        double Joint[6];
        for(int i = 0; i < 6; i++){
            Joint[i]=this->pBackend->pvisualRobot->mJointData[i].actJointPos;
        }
            this->uiRobotDataView->RoboticOpenViewUpdate(Joint);
    }
}
